package com.sntk.organauthomanager.infrastructure.dao;

import com.sntk.organauthomanager.domain.model.Role;
import com.sntk.organauthomanager.domain.model.RoleIdentify;
import com.sntk.organauthomanager.domain.repository.RoleRepository;
import com.sntk.organauthomanager.infrastructure.convert.RoleConvert;
import com.sntk.organauthomanager.infrastructure.db.entity.RoleEntity;
import com.sntk.organauthomanager.infrastructure.db.repository.RoleDBRepository;
import com.sntk.organauthomanager.infrastructure.exception.ObjectExistedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName: RoleRepositoryImpl
 * @author: wgchao
 * @createTime: 2020/7/21 3:43 PM
 * @Description: TODO
 */
@Component
public class RoleRepositoryImpl implements RoleRepository {
    @Autowired
    private RoleDBRepository roleDBRepository;
    @Autowired
    private RoleConvert roleConvert;

    @Override
    public void add(Role role, String operator) {
        RoleEntity roleEntity = roleDBRepository.findFirstByInstitutionNumberAndRoleCodeAndDelFalse(role.getRoleIdentify().getInstitutionNumber(),
                role.getRoleIdentify().getRoleCode());
        if(role.isAdd()){
            if(roleEntity != null){
                throw new ObjectExistedException("角色码已被使用");
            }
            roleEntity = roleConvert.convert(role);
            roleEntity.setDel(false);
            roleEntity.setCreator(operator);
            roleEntity.setCreateTime(new Date());
            roleEntity.setLastUpdateTime(new Date());
            roleEntity.setLastUpdator(operator);
            roleEntity = roleDBRepository.save(roleEntity);
        }else{
            if(roleEntity == null){
                throw new ObjectExistedException("角色不存在");
            }
            RoleEntity roleEntityNew = roleConvert.convert(role);
            roleEntityNew.setDel(roleEntity.getDel());
            roleEntityNew.setCreator(roleEntity.getCreator());
            roleEntityNew.setCreateTime(roleEntity.getCreateTime());
            roleEntityNew.setLastUpdateTime(new Date());
            roleEntityNew.setLastUpdator(operator);
            roleEntityNew = roleDBRepository.save(roleEntityNew);
        }
    }

    @Override
    public Role findByKey(RoleIdentify roleIdentify) {
        RoleEntity roleEntity = roleDBRepository.findFirstByInstitutionNumberAndRoleCodeAndDelFalse(roleIdentify.getInstitutionNumber(),
                roleIdentify.getRoleCode());
        if(roleEntity == null){
            return null;
        }
        return roleConvert.convert(roleEntity);
    }

    @Override
    public void delete(RoleIdentify roleIdentify, String operator) {
        RoleEntity roleEntity = roleDBRepository.findFirstByInstitutionNumberAndRoleCodeAndDelFalse(roleIdentify.getInstitutionNumber(),
                roleIdentify.getRoleCode());
        if(roleEntity == null){
            throw new ObjectExistedException("角色不存在");
        }
        roleEntity.setLastUpdator(operator);
        roleEntity.setLastUpdateTime(new Date());
        roleEntity.setDel(true);
        roleEntity = roleDBRepository.save(roleEntity);
    }
}
